package com.megvii.corelib.update.task;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

/**
 * Created by linyue on 16/3/6.
 */
public class UpdateDialog
{
    private UpdateDialogListener listener;

    public void show(final Context context, String version, String description,
            final boolean forceUpdate, final UpdateDialogListener listener)
    {
        String alert = "检查到最新版本的应用，版本号为" + version + "\n";
        if (forceUpdate)
        {
            alert = alert + "当前版本已不可用，如果不更新，将退出";
        }
        this.listener = listener;
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
        builder.setTitle("检查到新版本");
        builder.setMessage(alert + description);
        builder.setPositiveButton("更新", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                if (listener != null)
                {
                    listener.onClickUpdate();
                }
                dialog.cancel();
            }
        });
        builder.setNegativeButton(forceUpdate ? "退出" : "取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                if (listener != null)
                {
                    listener.onClickCancel();
                }
            }
        });
        builder.setCancelable(false);
        AlertDialog dialog = builder.show();
        dialog.setCanceledOnTouchOutside(false);
    }

    public interface UpdateDialogListener
    {
        public void onClickUpdate();

        public void onClickCancel();
    }
}
